if (!com) var com = {}
if (!com.logicpartners) com.logicpartners = {}
if (!com.logicpartners.designerTools) com.logicpartners.designerTools = {}

com.logicpartners.designerTools.barcode = function() {
  var self = this
  this.counter = 1
  this.button = $('<div></div>')
    .addClass('designerToolbarBarcode designerToolbarButton ')
    .attr('title', 'Barcode')
    .append($('<div></div>'))
  this.object = function(x, y, width, height) {
    this.toolName = 'barcode'
    this.props = {
      type: { label: '类型', default: '条形码' },
      name: { label: '元素', default: 'Barcode ' + self.counter++ },
      style: {
        label: '样式',
        default: 'CODE128',
        options: [
          { label: 'CODE128', value: 'CODE128' },
          { label: 'CODE128(A)', value: 'CODE128A' },
          { label: 'CODE128(C)', value: 'CODE128C' },
          { label: 'CODE39', value: 'CODE39' },
          { label: 'EAN13', value: 'EAN13' },
          { label: 'EAN8', value: 'EAN8' },
        ],
      },
      contentSet: {
        label: '内容设置',
        default: 'fixedContent',
        options: [
          { label: '固定内容', value: 'fixedContent' },
          { label: '数据来源', value: 'dataSource' },
        ],
      },
      text: { label: '内容', default: 'Textbox' },
      dataSourcefield: { label: '来源字段', default: 'name', hidden: true },
      x: { label: '起始位置x(mm)', default: x },
      y: { label: '起始位置y(mm)', default: y },
      width: { label: '宽度(mm)', default: 30 },
      height: { label: '高度(mm)', default: 5 },
      lineWidth: { label: '线条宽度(mm)', default: 0.2 },
      displayValue: {
        label: '显示文本',
        default: 'yes',
        options: [
          { label: '是', value: 'yes' },
          { label: '否', value: 'no' },
        ],
      },
      ZindexTop: { label: '置顶', button: '置顶' },
      ZindexBottom: { label: '置底', button: '置底' },
      ZindexUp: { label: '上移一层', button: '上移一层' },
      ZindexDown: { label: '下移一层', button: '下移一层' },
    }
    for (var prop in this.props) {
      this[prop] = this.props[prop].default
    }
    this.readonly = ['type', 'name', 'width', 'dataSourcefield']

    this.setWidth = function(width) {
      if (this.readonly && this.readonly.includes('width')) return
      this.width = precisionNumber(width)
    }

    this.getWidth = function() {
      return this.width
    }

    this.setHeight = function(height) {
      if (this.readonly && this.readonly.includes('height')) return
      this.height = precisionNumber(height)
    }

    this.getHeight = function() {
      return this.height
    }

    this.hitTest = function(coords) {
      return (
        coords.x >= this.x && coords.x <= this.x + this.width && coords.y >= this.y && coords.y <= this.y + this.height
      )
    }

    this.draw = function(context, scale) {
      this.x = precisionNumber(this.x)
      this.y = precisionNumber(this.y)
      this.setHeight(this.height)
      var content = this.text
      if (this.style == 'EAN13') content = '5901234123457'
      if (this.style == 'EAN8') content = '96385074'
      var tmpCanvas = $('<canvas></canvas>')[0]

      JsBarcode(tmpCanvas, content, {
        width: this.lineWidth * scale * 1.2, //设置条之间的宽度 //HARDCODE mybe related with dpi
        height: this.height * scale, //高度
        format: this.style, //选择要使用的条形码类型
        displayValue: this.displayValue == 'yes', //是否在条形码下方显示文字
        // text: '456', //覆盖显示的文本
        // fontOptions: 'bold italic', //使文字加粗体或变斜体
        // font: 'fantasy', //设置文本的字体
        // textAlign: 'left', //设置文本的水平对齐方式
        // textPosition: 'top', //设置文本的垂直位置
        textMargin: 2, //设置条形码和文本之间的间距
        fontSize: 18, //设置文本的大小
        // background: '#eee', //设置条形码的背景
        // lineColor: '#2196f3', //设置条和文本的颜色。
        margin: 5, //设置条形码周围的空白边距
      })
      var cwidth = tmpCanvas.width
      this.width = precisionNumber(cwidth / scale)
      context.drawImage(tmpCanvas, this.x, this.y, this.width, this.height)

      tmpCanvas.remove()
    }

    this.drawActive = function(context) {
      context.dashedStroke(this.x, this.y, this.x + this.width, this.y + this.height, [1, 0.5])
    }

    this.toZPL = function(labelwidth, labelheight) {
      var show = this.displayValue == 'yes' ? 'Y' : 'N'
      if (this.contentSet == 'fixedContent') {
        if (this.style == 'CODE128')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD${this.text}^FS`
        else if (this.style == 'CODE128A')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD>9${this.text}^FS`
        else if (this.style == 'CODE128C')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD>;${this.text}^FS`
        else if (this.style == 'CODE39')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^B3N,N,${this.height},${show}^FD${this.text}^FS`
        else if (this.style == 'EAN8')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^B8N,${this.height},${show}^FD${this.text}^FS`
        else if (this.style == 'EAN13')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BEN,${this.height},${show}^FD${this.text}^FS`
      } else {
        if (this.style == 'CODE128')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD{${this.dataSourcefield}}^FS`
        else if (this.style == 'CODE128A')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD>9{${this.dataSourcefield}}^FS`
        else if (this.style == 'CODE128C')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BCN,${this.height},${show}^FD>;{${this.dataSourcefield}}^FS`
        else if (this.style == 'CODE39')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^B3N,N,${this.height},${show}^FD{${this.dataSourcefield}}^FS`
        else if (this.style == 'EAN8')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^B8N,${this.height},${show}^FD{${this.dataSourcefield}}^FS`
        else if (this.style == 'EAN13')
          return `^FO${this.x},${this.y}^BY${this.lineWidth}^BEN,${this.height},${show}^FD{${this.dataSourcefield}}^FS`
      }
    }
  }
}
